<?php
/**
 * @author Minh Luan
 * @copyright 2012
 */


include_once("../service/CategoryService.php");
include_once("../service/ProductService.php");
include_once("../util/image/SimpleImage.php");
include_once("../util/ftp.php");

// just for upload images of product
function resize($newName, $proId) {
    if ($newName != '') {
        $path_source = "../images/product/PRO_".$proId."/".$newName;
        $image = new SimpleImage();
        $image->load($path_source);
        if ($image->getHeight() > 600 || $image->getWidth() > 800) {
            $image->scale(50);
            $image->save($path_source);
        }
    }
} 

if(isset($_POST['pro_name_en']) && isset($_POST['pro_name_vn'])){
    $pro_name_en = $_POST['pro_name_en'];
    $pro_name_vn = $_POST['pro_name_vn'];
    $description_en = $_POST['pro_description_en'];
    $description_vn = $_POST['pro_description_vn'];
    $pro_code = $_POST['pro_code'];
    $pro_price = $_POST['pro_price'];
    $cat_parent_id = $_POST['cat_parent_id'];
    $pro_status = $_POST['pro_status'];
    
    addProduct($pro_name_en, $pro_name_vn, $pro_code, $description_en, $description_vn, $pro_price, $cat_parent_id, $pro_status);
    $newestId = mysql_insert_id();
    if($newestId > 0){
		ftp_makeDir(getFTPRealPath()."images/product/PRO_".$newestId . "/");
        //Insert img1
        if($_FILES['img1']['name'] != ''){
            //Check valid image type
            if ((($_FILES["img1"]["type"] == "image/gif") || ($_FILES["img1"]["type"] == "image/jpeg") || ($_FILES["img1"]["type"] == "image/jpg")))
            {
                if ($_FILES["img1"]["error"] > 0)
                {
                    echo "Return Code: " . $_FILES["img1"]["error"] . "<br />";
                }
                else
                {
                    move_uploaded_file($_FILES["img1"]["tmp_name"],"../images/product/PRO_".$newestId."/1.jpg");
					resize("1.jpg", $newestId);
                }
                addImageOfProduct($newestId, "1.jpg", $pro_code);
            }
            else
            {
                echo "Invalid file";
                echo "Upload: " . $_FILES["img1"]["name"] . "<br />";
                echo "Type: " . $_FILES["img1"]["type"] . "<br />";
                echo "Size: " . ($_FILES["img1"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["img1"]["tmp_name"] . "<br />"; 
            }
        }else{
            addImageOfProduct($newestId, "", $pro_code);
        }
        //Insert img2
        if($_FILES['img2']['name'] != ''){
            //Check valid image type
            if ((($_FILES["img2"]["type"] == "image/gif") || ($_FILES["img2"]["type"] == "image/jpeg") || ($_FILES["img2"]["type"] == "image/jpg")))
            {
                if ($_FILES["img2"]["error"] > 0)
                {
                    echo "Return Code: " . $_FILES["img2"]["error"] . "<br />";
                }
                else
                { 
                    move_uploaded_file($_FILES["img2"]["tmp_name"],"../images/product/PRO_".$newestId."/2.jpg");
					resize("2.jpg", $newestId);
                }
                addImageOfProduct($newestId, "2.jpg", $pro_code);
            }
            else
            {
                echo "Invalid file";
                echo "Upload: " . $_FILES["img2"]["name"] . "<br />";
                echo "Type: " . $_FILES["img2"]["type"] . "<br />";
                echo "Size: " . ($_FILES["img2"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["img2"]["tmp_name"] . "<br />"; 
            }
        }else{
            addImageOfProduct($newestId, "", $pro_code);
        }
        //Insert img3
        if($_FILES['img3']['name'] != ''){
            //Check valid image type
            if ((($_FILES["img3"]["type"] == "image/gif") || ($_FILES["img3"]["type"] == "image/jpeg") || ($_FILES["img3"]["type"] == "image/jpg")))
            {
                if ($_FILES["img3"]["error"] > 0)
                {
                    echo "Return Code: " . $_FILES["img3"]["error"] . "<br />";
                }
                else
                {
                    move_uploaded_file($_FILES["img3"]["tmp_name"],"../images/product/PRO_".$newestId."/3.jpg");
					resize("3.jpg", $newestId);
                }
                addImageOfProduct($newestId, "3.jpg", $pro_code);
            }
            else
            {
                echo "Invalid file";
                echo "Upload: " . $_FILES["img3"]["name"] . "<br />";
                echo "Type: " . $_FILES["img3"]["type"] . "<br />";
                echo "Size: " . ($_FILES["img3"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["img3"]["tmp_name"] . "<br />"; 
            }
        }else{
            addImageOfProduct($newestId, "", $pro_code);
        }
        //Insert img4
        if($_FILES['img4']['name'] != ''){
            //Check valid image type
            if ((($_FILES["img4"]["type"] == "image/gif") || ($_FILES["img4"]["type"] == "image/jpeg") || ($_FILES["img4"]["type"] == "image/jpg")))
            {
                if ($_FILES["img4"]["error"] > 0)
                {
                    echo "Return Code: " . $_FILES["img4"]["error"] . "<br />";
                }
                else
                {
                    move_uploaded_file($_FILES["img4"]["tmp_name"],"../images/product/PRO_".$newestId."/4.jpg");
					resize("4.jpg", $newestId);
                }
                addImageOfProduct($newestId, "4.jpg", $pro_code);
            }
            else
            {
                echo "Invalid file";
                echo "Upload: " . $_FILES["img4"]["name"] . "<br />";
                echo "Type: " . $_FILES["img4"]["type"] . "<br />";
                echo "Size: " . ($_FILES["img4"]["size"] / 1024) . " Kb<br />";
                echo "Temp file: " . $_FILES["img4"]["tmp_name"] . "<br />"; 
            }
        }else{
            addImageOfProduct($newestId, "", $pro_code);
        }
        
        echo "<script>location.href='../admin/product.php?msg=4'</script>";
    }else{
        echo "<script>location.href='../admin/product-detail.php?do=add&msg=2'</script>";       
    }
}else{
    echo "<script>location.href='../admin/product-detail.php?do=add&msg=2'</script>";
}

?>